//
//  RegisterViewController.m
//  test2
//
//  Created by 贺银银 on 16/3/26.
//  Copyright © 2016年 hyy. All rights reserved.
//

#import "RegisterViewController.h"
#import "ChatListViewController.h"
#import <CDChatManager.h>
#import "SVProgressHUD.h"
#import "LoginViewController.h"
#import "LZAlertViewHelper.h"
@interface RegisterViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;
@property (weak, nonatomic) IBOutlet UITextField *checkNumber;
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;

@property (weak, nonatomic) IBOutlet UITextField *passWordTextField;


- (IBAction)registerAction:(UIButton *)sender;
- (IBAction)checkPhoneAction:(UIButton *)sender;
@property (nonatomic,strong)LZAlertViewHelper *alertViewHelper;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.phoneNumber.delegate = self;
    self.checkNumber.delegate = self;
    
    
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)registerAction:(UIButton *)sender {
    
    [AVOSCloud verifySmsCode:self.checkNumber.text mobilePhoneNumber:self.phoneNumber.text callback:^(BOOL succeeded, NSError *error) {
        
        if (succeeded) {
            
            // regis action
            AVUser * user = [AVUser user];
            
            user.password = self.passWordTextField.text;
            user.username = self.userNameTextField.text;
            user.mobilePhoneNumber = self.phoneNumber.text;
            [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                
                if (succeeded) {
                    
                    NSLog(@"注册成功");
                    
                    [[CDChatManager manager]openWithClientId:self.phoneNumber.text callback:^(BOOL succeeded, NSError *error) {
                            ChatListViewController * chatList = [[ChatListViewController alloc]init];
                            [self.navigationController pushViewController:chatList animated:YES];
                        
                    }];
                    
                    [self dismissViewControllerAnimated:YES completion:nil];
                    
                } else {
                    
                    [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
                }
            }];
        } else {
            
            [SVProgressHUD showErrorWithStatus:@"输入正确的验证码"];
        }
    }];
    
    
}

- (IBAction)checkPhoneAction:(UIButton *)sender {
    
    [AVOSCloud requestSmsCodeWithPhoneNumber:self.phoneNumber.text appName:nil operation:nil timeToLive:0 callback:^(BOOL succeeded, NSError *error) {
        
        if (succeeded) {
            
            [SVProgressHUD showSuccessWithStatus:@"验证码发送成功"];
        }else{
            NSLog(@"%@",error);
            [SVProgressHUD showSuccessWithStatus:@"验证码发送失败"];
        }
    }];
    
}
@end
